package com.richard.cloud;

import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richard.cloud.entity.Storage;
import com.richard.cloud.repo.StorageRepo;
import com.richard.cloud.service.StorageService;
import com.richard.springcloud.base.ComnResult;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class BootSeataStorageService9005 {
	public static void main(String[] args) {
		SpringApplication.run(BootSeataStorageService9005.class, args);
	}
}

@Slf4j
@RestController
class TestCtrller {
	@Resource
	private StorageService service;
	@Resource
	private StorageRepo repo;
	
	@GetMapping("/hello")
	public String hello() {
		return "storage-service " +LocalDateTime.now().toString();
	}
	
	@PutMapping("/storage/decrease")
	public ComnResult<?> decrease(@RequestParam Long prodId, @RequestParam Integer count) {
		log.info(">> 扣减库存开始");
//		service.decreaseStock(prodId, count);
		Storage entity = repo.findByProdId(prodId);
		entity.setStock(entity.getStock() - count);
		repo.saveAndFlush(entity);
		log.info("<< 扣减库存结束");
		
		return ComnResult.succ();
	}
}